package cn.school.controller.dashboard;

import cn.school.classManage.service.SchoolClassService;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.entity.StudentInfo;
import cn.school.student.service.StudentInfoService;
import cn.school.sys.entity.SysUser;
import cn.school.sys.service.SysUserService;
import cn.school.workflow.service.LeaveApplyService;
import cn.school.workflow.service.ViolativeApplyService;
import cn.school.workflow.vo.ViolativeApplyVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人数统计接口
 *
 * @className: PopulationStatistics
 * @author: fangjin.tao
 * @date: 2022/12/12
 **/
@RestController
@Api(tags = "PopulationStatistics => 人数统计接口")
@RequestMapping("/dashboard/population/statistics")
public class PopulationStatisticsController {
	@Autowired
	private StudentInfoService studentInfoService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private ViolativeApplyService violativeApplyService;
	@Autowired
	private SchoolClassService schoolClassService;

	@Autowired
	private LeaveApplyService leaveApplyService;

	@ApiOperation("获取统计数据")
	@GetMapping("/countInfo")
	public CommonResult<Map<String, Integer>> list(@RequestParam Map<String, Object> para) {
		// 平台学生(有效)-总数量
		String tenantId = SysUserUtils.getSysUser().getTenantId();
		LambdaQueryWrapper<StudentInfo> studentCountWrapper = new LambdaQueryWrapper<>();
		studentCountWrapper.eq(StudentInfo::getStatus, 1).eq(StudentInfo::getTenantId,tenantId);
		Integer studenCount = this.studentInfoService.count(studentCountWrapper);

		// 微信公众号授权绑定数量
		LambdaQueryWrapper<StudentInfo> parentBindCountWrapper = new LambdaQueryWrapper<>();
		parentBindCountWrapper.eq(StudentInfo::getStatus, 1).eq(StudentInfo::getTenantId,tenantId)
				.isNotNull(StudentInfo::getGuardianOfficialOpenId);
		Integer parentBindCount = this.studentInfoService.count(parentBindCountWrapper);

		// 平台违纪(正常结束通过)-总数量
		LambdaQueryWrapper<ViolativeApplyVO> violativeApplyCountWrapper = new LambdaQueryWrapper<>();
		violativeApplyCountWrapper.eq(ViolativeApplyVO::getApplyStatus, 2);
		violativeApplyCountWrapper.or();
		violativeApplyCountWrapper.eq(ViolativeApplyVO::getApplyStatus, 3);
		Integer violativeApplyCount = this.violativeApplyService.count(violativeApplyCountWrapper);
		// 平台用户(有效)-总数量
		LambdaQueryWrapper<SysUser> sysUserCountWrapper = new LambdaQueryWrapper<>();
		sysUserCountWrapper.eq(SysUser::getStatus, 1);
		Integer sysUserCount = this.sysUserService.count(sysUserCountWrapper);
		// 平台班级-总数量
		LambdaQueryWrapper<SchoolClassVO> schoolClassCountWrapper = new LambdaQueryWrapper<>();
		schoolClassCountWrapper.eq(SchoolClassVO::getStatus, 1);
		schoolClassCountWrapper.ne(SchoolClassVO::getIsGraduation,"1")
				.eq(SchoolClassVO::getTenantId,tenantId);
		Integer schoolClassCount = this.schoolClassService.count(schoolClassCountWrapper);
		// 封装结果
		Map<String, Integer> resultCountMap = new HashMap<>();
		resultCountMap.put("studenCount", studenCount);
		resultCountMap.put("violativeApplyCount", violativeApplyCount);
		resultCountMap.put("sysUserCount", sysUserCount);
		resultCountMap.put("schoolClassCount", schoolClassCount);
		resultCountMap.put("parentBindCount",parentBindCount);

		return CommonResult.success(resultCountMap);
	}

	@ApiOperation("获取按天分组统计：违纪记录-最近七天")
	@GetMapping("/violativeApplyLast/LastSenvenDayGroup")
	public CommonResult<Map<String, List<Object>>> violativeApplyLastSenvenDayList(@RequestParam Map<String, Object> para) {
		// 平台学生(有效)-总数量
		Map<String, List<Object>> resultMap = this.violativeApplyService.violativeApplyLastSenvenDayList();
		return CommonResult.success(resultMap);
	}


	@ApiOperation("获取按天分组统计：教职工请假记录-最近30天")
	@GetMapping("/LeaveApply/LastOneMonthGroup")
	public CommonResult<Map<String, List<Object>>> leaveApplyLastOneMonthGroup(@RequestParam Map<String, Object> para) {
		// 平台都职工请假(有效)
		Map<String, List<Object>> resultMap = this.leaveApplyService.leaveApplyLastOneMonthGroup();
		return CommonResult.success(resultMap);
	}


}
